package com.xupypr.surgery.client;

import static com.google.gwt.dom.client.Style.Unit.PCT;
import static com.google.gwt.dom.client.Style.Unit.PX;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.xupypr.surgery.client.objects.BlogEntry;

public class EditorWidget extends Composite {
	
	private final AdminPanel adminPanel;
	
	private LayoutPanel panel = new LayoutPanel();
	private TextBox caption = null;
	private TextArea text = null;
	private TextArea cuttedText = null;
	private Long editedEntryId = null;
	
	public EditorWidget(AdminPanel ap) {
		adminPanel = ap;
		
		FlowPanel child0 = new FlowPanel(), child1  = new FlowPanel(), child2  = new FlowPanel();
		panel.add(child0); panel.add(child1); panel.add(child2);

		panel.setWidgetLeftWidth(child0, 0, PX, 100, PCT);
		panel.setWidgetTopHeight(child0, 0, PX, 50, PX);
		
		panel.setWidgetLeftWidth(child1, 0, PX, 100, PCT);
		panel.setWidgetTopHeight(child1, 50, PX, 100, PX);

		panel.setWidgetLeftWidth(child2, 0, PX, 100, PCT);
		panel.setWidgetTopHeight(child2, 150, PX, 100, PCT);
		
		caption = new TextBox();
		caption.setWidth("100%");
		text = new TextArea();
		text.setWidth("100%");
		cuttedText = new TextArea();
		cuttedText.setWidth("100%");
		Button addButton = new Button();
		addButton.setText("Сохранить");
		addButton.addClickHandler(getCreateHandler());
				
		child0.add(caption);
		child1.add(text);
		child2.add(cuttedText);
		child2.add(addButton);
		
		initWidget(panel);
	}

	public TextBox getCaption() {
		return caption;
	}

	public void setCaption(TextBox caption) {
		this.caption = caption;
	}

	public TextArea getText() {
		return text;
	}

	public void setText(TextArea text) {
		this.text = text;
	}

	public TextArea getCuttedText() {
		return cuttedText;
	}

	public void setCuttedText(TextArea cuttedText) {
		this.cuttedText = cuttedText;
	}
	
	private ClickHandler getCreateHandler() {
		ClickHandler createEntryHandler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				BlogEntry entry = new BlogEntry();
				entry.setPublicated(false);
				entry.setCreated(new Date());
				entry.setCaption(caption.getText());
				entry.setText(text.getText());
				entry.setCuttedText(cuttedText.getText());
				entry.setId(editedEntryId);
				adminPanel.getDispatcher().createEntry(entry, new AsyncCallback<Void>() {

					@Override
					public void onSuccess(Void result) {
						adminPanel.switchMode();
						adminPanel.refreshEntriesList(editedEntryId);
						// TODO отправить мессагу админке о появлении нового элемента
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
					}
				});
			}
		};
		return createEntryHandler;
	}

	public void loadEntry(Long id) {
		if (id==null) {
			editedEntryId = null;
		 	getCaption().setText("");
		 	getText().setText("");
		 	getCuttedText().setText("");
		} else {
		editedEntryId = id;	
		adminPanel.getDispatcher().getEntry(id, new AsyncCallback<BlogEntry>() {
			
			@Override
			public void onSuccess(BlogEntry result) {
				getCaption().setText(result.getCaption());
				getText().setText(result.getText());
				getCuttedText().setText(result.getCuttedText());
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
			}
		});
		}
	}
}
